import _ from 'lodash';
import axios from "axios";
import qs from 'qs';
// 引入store
import store from '../store';
import md5 from 'blueimp-md5';

//md5加密
// export function doMd5(val) {
//   return md5(md5(new Date().getTime() +val));
// }
export function doMd5(val) {
  return md5(val);
}


//获取lodash工具类
export function lodash() {
  return _;
}

/**
 * 获取api路径出去https或http:只要ip地址
 */
export function getIp(){
  let apiUrl=getApiIp();
  var index=apiUrl.indexOf(":");
  if(index==-1){
    return apiUrl
  }
  return apiUrl.substring(index+3,apiUrl.length);
}


/**
 * 获取Aip接口id
 * @returns {string}
 */
export function getApiIp() {
  return process.env.VUE_APP_API_URL;
}


//获取token
export function getToken() {
  return store.state.userInfo.token;
}


/**
 * 移除avue自动生成的$开头的参数
 * @param obj
 */
export function del$(obj){
  let row=copyBean(obj);
  for(let key in row){
    if(key.indexOf("$")==0){
      delete row[key];
    }
  }
  return row;
}

/**
 * axios封装请求
 * @param options 选项
 */
export function req(options) {

  let opt = _.merge({
    method: 'get'
  }, options);
  return axios(opt)
    // 请求成功
    .then(function (resp) {
      return resp.data.code == 200?resp.data.data:null;
    });
}


/**
 *  axios delete请求
 * @param url
 * @param params
 * @returns {*}
 */
export function del(url, params) {
  let options = {
    url: url,
    params: params,
    method: 'DELETE'
  };
  return req(options);
}

/**
 *  axios get请求
 * @param url 请求地址
 * @param params 参数
 */
export function get(url, params) {
  let options = {
    url: url,
    params: params,
    method: 'GET'
  };
  return req(options);
}

/**
 * axios post请求
 * @param url
 * @param params
 * @returns {*}
 */
export function post(url, params) {
  let options = {
    url: url,
    data:params,
    method: 'POST'
  };
  return req(options);
}
/**
 * axios put请求
 * @param url
 * @param params
 * @returns {*}
 */
export function put(url, params) {
  let options = {
    url: url,
    data: params,
    method: 'put'
  };
  return req(options);
}


/**
 * axios 提交表单请求
 * @param options 选项
 * @returns {*}
 */
export function reqForm(options) {
  if (options.data) {
    options.data = qs.stringify(options.data);
  }
  return req(options);
}


